package com.ng.framework.view.desktop;

import java.awt.Component;
import java.awt.Dimension;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import net.sf.jasperreports.engine.JRException;

import com.ng.framework.report.NGJasperViewer;
import com.ng.framework.report.NGReport;

public abstract class NGTable extends JPanel {

	private static final long serialVersionUID = -8636470648120314248L;

	private TableCellRenderer headerRenderer = new NGTableHeaderRenderer();

	private TableCellRenderer cellRenderer = new NGTableCellRenderer();

	private Border headerBorder = UIManager.getBorder("TableHeader.cellBorder");

	protected List data;

	public abstract AbstractTableModel getEntityModel();

	public abstract int getSelectedRow();

	public abstract int[] getSelectedRows();

	public abstract void setData(List data);

	public void updateColumnRenderer(TableColumn column, Object columnValue, Integer alignment, Integer expectedValuesLenght) {

		JLabel columnLabel = new JLabel(columnValue.toString(), null, alignment);
		columnLabel.setBorder(headerBorder);

		column.setHeaderValue(columnLabel);
		column.setHeaderRenderer(headerRenderer);

		column.setCellRenderer(cellRenderer);

		Integer width = 6 * expectedValuesLenght;

		column.setPreferredWidth(width);
	}

	public void report(NGReport ngReport) {
		NGJasperViewer jasperViewer = new NGJasperViewer(null, false, Locale.getDefault());
		jasperViewer.setDefaultCloseOperation(jasperViewer.DISPOSE_ON_CLOSE);
		jasperViewer.setAlwaysOnTop(false);
		jasperViewer.setSize(new Dimension(480, 600));

		try {
			ngReport.setObjects(this.data);
			ngReport.buildReport();
		} catch (JRException e) {
			e.printStackTrace();
		}

		jasperViewer.viewReport(ngReport.getJasperPrint());
	}
}

class NGTableHeaderRenderer implements TableCellRenderer {
	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
			int column) {
		return (JComponent) value;
	}
}

class NGTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer {

	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
			int column) {

		super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

		if (value instanceof Long || value instanceof Integer || value instanceof Date || value instanceof String) {
			return renderCell(this, String.valueOf(value), SwingConstants.CENTER);
		} else if (value instanceof Double || value instanceof Float) {
			return renderCell(this, String.valueOf(value), SwingConstants.RIGHT);
		}

		return new JLabel(String.valueOf(value));
	}

	private JLabel renderCell(JLabel render, String value, int align) {
		render.setText(value);
		render.setHorizontalAlignment(align);
		return render;
	}

}